package de.tum.in.eist.poll.client;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.shared.Result;

/***
 * view for student to participate in the survey
 */
public class StudentSurveyView {
	static void paint(){
		if(Poll.student == null){
			History.newItem("studentLogin");
			return;
		}
		RootPanel.get("logo").addStyleName("logo-small");

		final VerticalPanel surveyContainer = new VerticalPanel();
		surveyContainer.addStyleName("surveyContainer");
		class DateHandler implements ClickHandler, KeyUpHandler {
			private Date date;
			public DateHandler(Date d){
				date = d;
			}
			public void onClick(ClickEvent event) {
				changeUI();
			}
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					changeUI();
				}
			}
			public void changeUI() {
				Poll.currentLecDate = date;
				surveyContainer.clear();
				final VerticalPanel lecPanel = new VerticalPanel();
				lecPanel.addStyleName("lecPanel");
				DateTimeFormat format = DateTimeFormat.getFormat("EEEE, d MMM yyyy 'at' HH:mm");
				for(de.tum.in.eist.poll.shared.Poll d : Poll.student.getAvailablePolls()){
					Button lec = new Button(format.format(d.getDate()));
					lec.setStyleName("lecDateButton");
					if(d.getDate() == Poll.currentLecDate)
						lec.addStyleName("active");
					DateHandler dh = new DateHandler(d.getDate());
					lec.addClickHandler(dh);
					lecPanel.add(lec);
				}
				surveyContainer.add(lecPanel);
				// Paint the Survey
				final FlowPanel q1Panel = new FlowPanel();
				final FlowPanel q2Panel = new FlowPanel();
				final FlowPanel q3Panel = new FlowPanel();

				// Labels for the questions
				final Label q1Label = new Label("How satisfied were you with the lecture on "+format.format(Poll.currentLecDate)+" o'clock?");
				final Label q2Label = new Label("What did you like most?");
				final Label q3Label = new Label("What did you dislike or did not understand at all?");
				// Radio Buttons for Q1
				final RadioButton rb0 = new RadioButton("Question1", "Very Satisfied");
				final RadioButton rb1 = new RadioButton("Question1", "Satisfied");
				final RadioButton rb2 = new RadioButton("Question1", "Neutral");
				final RadioButton rb3 = new RadioButton("Question1", "Unsatisfied");
				final RadioButton rb4 = new RadioButton("Question1", "Very Unsatisfied");
				// Add buttons to RadioButtonPanel of question 1
				q1Panel.add(q1Label);
				q1Panel.add(rb0);
				q1Panel.add(rb1);
				q1Panel.add(rb2);
				q1Panel.add(rb3);
				q1Panel.add(rb4);
				// Question2
				q2Panel.add(q2Label);
				final TextBox q2Box = new TextBox();
				q2Panel.add(q2Box);
				// Question3
				q3Panel.add(q3Label);
				final TextBox q3Box = new TextBox();
				q3Panel.add(q3Box);
				// Create submit-button
				final Button submitButton = new Button("Submit Answers");
				
				class SubmitHandler implements ClickHandler, KeyUpHandler {
					public void onClick(ClickEvent event) {
						surveyToServer();
					}
					public void onKeyUp(KeyUpEvent event) {
						if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
							surveyToServer();
						}
					}
					public void surveyToServer() {
						// sends the button-number which was clicked
						int q1 = 0;
						// set int q1 to the number which radiobutton was clicked
						if (rb0.getValue())
							q1 = 2;
						else if (rb1.getValue())
							q1 = 1;
						else if (rb2.getValue())
							q1 = 0;
						else if (rb3.getValue())
							q1 = -1;
						else if (rb4.getValue())
							q1 = -2;

						// String of question 2 store in variable q2 for sending to server
						String q2 = q2Box.getText();
						// String of question 3 store in variable q3 for sending to server
						String q3 = q3Box.getText();

						// change value of onFailur and onSucces
						// server-function to store the answers
						Result result = new Result(Poll.currentLecDate, q1, q2, q3);
						Poll.pollService.submitPoll(result, Poll.student, new AsyncCallback<Void>() {
							public void onFailure(Throwable caught) {
							}

							public void onSuccess(Void ret) {
								DialogBox db = new DialogBox();
								Label dbLbl = new Label("Your answer was sent!");
								db.add(dbLbl);
								db.center();
							}
						});
					}
				}

				// Add SubmitHandler to button to send answers to the server
				SubmitHandler submit_handler = new SubmitHandler();
				submitButton.addClickHandler(submit_handler);

				// Add widgets to RootPanel
				surveyContainer.add(q1Panel);
				surveyContainer.add(q2Panel);
				surveyContainer.add(q3Panel);
				surveyContainer.add(submitButton);
			}
		}
		if(Poll.currentLecDate == null){
			Poll.currentLecDate = Poll.student.getAvailablePolls().get(0).getDate();
		}
		DateHandler dh = new DateHandler(Poll.currentLecDate);
		dh.changeUI();
		RootPanel.get("container").add(surveyContainer);
	}
}
